﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for KPIChildAttachmentSerializer
    /// </summary>
    public class KPIChildAttachmentSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                   "Jan",
                   "Feb",
                   "Mar",
                   "Apr",
                   "May",
                   "Jun",
                   "Jul",
                   "Aug",
                   "Sep",
                   "Oct",
                   "Nov",
                   "Dec"
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            int year = Convert.ToInt32(HttpContext.Current.Request.Params["Year"]);
            int KPIID = Convert.ToInt32(HttpContext.Current.Request.Params["KPI"]);
            int userid = Convert.ToInt32(HttpContext.Current.Request.Params["User"]);

            IList<KPIChildAttachment> result = new List<KPIChildAttachment>();
            KPIAttachmentFacade pf = new KPIAttachmentFacade();
            int iTotalRecords = 1;
            result = pf.GetKPIChildAttachment(new KPI() { ID = KPIID }, year, new Employee() { ID = userid});
            int iTotalDisplayRecords = 1;

            iTotalDisplayRecords = iTotalRecords;

            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();

            if (result != null && result.Count > 0)
            {
                string Jan = string.Empty;
                string Feb = string.Empty;
                string Mar = string.Empty;
                string Apr = string.Empty;
                string May = string.Empty;
                string Jun = string.Empty;
                string Jul = string.Empty;
                string Aug = string.Empty;
                string Sep = string.Empty;
                string Oct = string.Empty;
                string Nov = string.Empty;
                string Dec = string.Empty;
                
                foreach (KPIChildAttachment x in result)
                {
                    switch (x.Bulan.ToLower())
                    {
                        case "jan":
                            Jan +=  x.File + " \n ";
                           
                            break;
                        case "feb":
                            Feb += x.File + " \n ";
                            break;
                        case "mar":
                            Mar += x.File + " \n ";
                            break;
                        case "apr":
                            Apr += x.File + " \n ";
                            break;
                        case "may":
                            May += x.File + " \n ";
                            break;
                        case "jun":
                            Jun +=  x.File + " \n ";
                            break;
                        case "jul":
                            Jul += x.File + " \n ";
                            break;
                        case "aug":
                            Aug += x.File + " \n ";
                            break;
                        case "sep":
                            Sep += x.File + " \n ";
                            break;
                        case "oct":
                            Oct += x.File + " \n ";
                            break;
                        case "nov":
                            Nov += x.File + " \n ";
                            break;
                        case "dec":
                            Dec += x.File + " \n ";
                            break;
                        default: break;
                    }
                }

                IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                ColumnValues.Add(this.Columns[0], Jan == string.Empty ? string.Empty : Jan);
                ColumnValues.Add(this.Columns[1], Feb == string.Empty ? string.Empty : Feb);
                ColumnValues.Add(this.Columns[2], Mar == string.Empty ? string.Empty : Mar);
                ColumnValues.Add(this.Columns[3], Apr == string.Empty ? string.Empty : Apr);
                ColumnValues.Add(this.Columns[4], May == string.Empty ? string.Empty : May);
                ColumnValues.Add(this.Columns[5], Jun == string.Empty ? string.Empty : Jun);
                ColumnValues.Add(this.Columns[6], Jul == string.Empty ? string.Empty : Jul);
                ColumnValues.Add(this.Columns[7], Aug == string.Empty ? string.Empty : Aug);
                ColumnValues.Add(this.Columns[8], Sep == string.Empty ? string.Empty : Sep);
                ColumnValues.Add(this.Columns[9], Oct == string.Empty ? string.Empty : Oct);
                ColumnValues.Add(this.Columns[10], Nov == string.Empty ? string.Empty : Nov);
                ColumnValues.Add(this.Columns[11], Dec == string.Empty ? string.Empty : Dec);
                collection.Add(1, ColumnValues);
            }


            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }
    }
}